package com.zyl.pagergallery_kt

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.zyl.pagergallery_kt.adapter.PhotoAdapter
import com.zyl.pagergallery_kt.pojo.PhotoItem
import kotlinx.android.synthetic.main.fragment_photo.*


class PhotoFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_photo, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val photoList = arguments?.getParcelableArrayList<PhotoItem>("PHOTO_LIST")

        PhotoAdapter().apply {
            viewPager2.adapter = this
            submitList(photoList)
        }

        //滑动时，下面的textView也改变
        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                textView.text = "${position + 1} / ${photoList?.size}"
            }
        })

        //设置当前点击的图片
        viewPager2.setCurrentItem(arguments?.getInt("PHOTO_POSITION") ?: 0, false)

    }

}